//
//  WWRequestAPI+WWAccountAPI.m
//  WangLottery
//
//  Created by 黄修勇 on 12-9-10.
//
//

#import "WWRequestAPI+WWAccountAPI.h"

@implementation WWRequestAPI (WWAccountAPI)

-(long)loginWithusername:(NSString *)username psw:(NSString *)psw target:(id)target{

    if (!username||!psw) {
        [NSException exceptionWithName:@"LoginExceotion" reason:@"登陆的用户名或者密码不能为空" userInfo:nil];
        return -1;
    }
    NSDictionary *requestPara=[NSDictionary dictionaryWithObjectsAndKeys:username,@"name",psw,@"pwd", nil];
    return [self requestWithAction:kLOGIN_ACTION paras:requestPara target:target parserSEL:@selector(loginDicForString:)];
}

-(long)forgotPswByTel:(NSString *)tel target:(id)target{

    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:tel,@"tel", nil];
    return [self requestWithAction:kFORGOT_PSW_ACTION paras:dic target:target parserSEL:@selector(simpleResultForString:)];
}

-(long)registWithName:(NSString *)name phone:(NSString *)phone psw:(NSString *)psw target:(id)target{

    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:name,@"name",phone,@"phone",psw,@"pwd", nil];
    return [self requestWithAction:KREGISTER_ACTION paras:dic target:target parserSEL:@selector(simpleResultForString:)];
}

-(long)modifyDetailInfoWithOldPhone:(NSString *)oldPhone oldPsw:(NSString *)oldPsw email:(NSString *)email username:(NSString *)username cardID:(NSString *)cartID newPhone:(NSString *)newPhone newPsw:(NSString *)newPsw target:(id)target{

    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
                       oldPhone,@"tname",oldPsw,@"tpwd",
                       email,@"mail",username,@"truename",
                       cartID,@"idcard",newPhone,@"phone",newPsw,@"pwd", nil];
    return [self requestWithAction:@"modreg" paras:dic target:target parserSEL:@selector(simpleResultForString:)];
}

-(long)modifyNewPswWithTel:(NSString *)tel code:(NSString *)code newPassWord:(NSString *)psw target:(id)target{

    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:tel,@"tel",code,@"code",psw,@"password", nil];
    return [self requestWithAction:@"setnewpwd" paras:dic target:target parserSEL:@selector(simpleResultForString:)];
}
@end



@implementation WWDataParser (WWAccountParser)


-(NSDictionary *)loginDicForString:(NSString *)string{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    id tempResult=[string parserData];
    if ([tempResult isKindOfClass:[NSString class]]) {
        NSArray * tempArray=[tempResult componentsSeparatedByString:@","];
        if (tempArray.count!=0) {
            [dic setObject:[tempArray objectAtIndex:0] forKey:@"result"];
            [dic setObject:[tempArray objectAtIndex:1] forKey:@"reason"];
            if (tempArray.count>2) {
                [dic setObject:[tempArray objectAtIndex:2] forKey:@"phone"];
                [dic setObject:[tempArray objectAtIndex:3] forKey:@"total money"];
                [dic setObject:[tempArray objectAtIndex:4] forKey:@"rozen money"];
                [dic setObject:[tempArray objectAtIndex:5] forKey:@"name"];
                [dic setObject:[tempArray objectAtIndex:6] forKey:@"ID num"];
                [dic setObject:[tempArray objectAtIndex:7] forKey:@"lottery money"];
                [dic setObject:[tempArray objectAtIndex:8] forKey:@"carry money"];
            }
        }
    }
    
    return [self dicForResult:dic];
}

@end